package com.controller;

import com.ql.pojo.user.DataGridView;
import com.ql.pojo.user.WorkFlowVo;
import com.ql.pojo.yk.Ypcg;
import com.ql.service.user.WorkFlowService;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WorkFlowController {
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private FormService formService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private ManagementService managementService;

    @Autowired
    private WorkFlowService workFlowService;
    @RequestMapping(value = "/test1")
    @ResponseBody
    public String test(){
        Deployment deploy = repositoryService.createDeployment().name("请假流程").
                addClasspathResource("shenqing.bpmn").
                addClasspathResource("shenqing.png").deploy();
        System.out.println("部署成功：流程部署ID:"+deploy.getId());
        RepositoryService R = repositoryService;
        System.out.println(R.toString());
        return "测试成功";
    }

    @RequestMapping(value = "toWorkFlowManager")
    public String toWorkFlowManager(){
        return "";
    }

    /**
     * 加载部署信息数据
     */

    @RequestMapping(value = "loadAllDeployment",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public DataGridView loadAllDeployment(WorkFlowVo workFlowVo){
        DataGridView result = workFlowService.queryProcessDeploy(workFlowVo);
        //return JSON.toJSONString(result);
        return result;
    }

    /**
     * 加载流程定义信息
     */

    @RequestMapping(value = "loadAllProcessDefinition",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public DataGridView loadAllProcessDefinition(WorkFlowVo workFlowVo){
        DataGridView result = workFlowService.queryAllProcessDefinition(workFlowVo);
        return result;
    }

    @RequestMapping("toAddWorkFlow")
    public String toAddWorkFlow(){
        return "WorkFlowAdd";
    }

    @RequestMapping("addWorkFlow")
    @ResponseBody
    public Map<String,Object> addWorkFlow(MultipartFile mf,WorkFlowVo workFlowVo){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
           this.workFlowService.addWorkFlow(mf.getInputStream(),workFlowVo.getDeploymentName());
            map.put("msg","部署成功");
        }catch (Exception e){
            map.put("msg","部署失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     *删除部署信息
     * @param workFlowVo
     * @return
     */
    @RequestMapping("deleteWorkFlow")
    @ResponseBody
    public Map<String,Object> deleteWorkFlow(WorkFlowVo workFlowVo){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            this.workFlowService.deleteWorkFlow(workFlowVo.getDeploymentId());
            map.put("msg","删除成功");
        }catch (Exception e){
            map.put("msg","删除失败");
            e.printStackTrace();
        }
        return map;
    }


    /**
     *删除部署信息
     * @param workFlowVo
     * @return
     */
    @RequestMapping("batchDeleteWorkFlow")
    @ResponseBody
    public Map<String,Object> batchDeleteWorkFlow(WorkFlowVo workFlowVo){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            String [] deploymentIds = workFlowVo.getIds();
            for (String deploymentId:
                    deploymentIds) {
                this.workFlowService.deleteWorkFlow(deploymentId);
            }
            map.put("msg","删除成功");
        }catch (Exception e){
            map.put("msg","删除失败");
            e.printStackTrace();
        }
        return map;
    }


    @RequestMapping("toViewProcessImage")
    public String toViewProcessImage(WorkFlowVo workFlowVo){
        return "viewProcessImage";
    }

    @RequestMapping("viewProcessImage")
    public void viewProcessImage(WorkFlowVo workFlowVo, HttpServletResponse response){
        InputStream stream = this.workFlowService.queryProcessDeploymentImage(workFlowVo.getDeploymentId());
        try {
            BufferedImage image = ImageIO.read(stream);
            ServletOutputStream outputStream =  response.getOutputStream();
            ImageIO.write(image,"png",outputStream);
            stream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     *启动流程
     * @param cgId
     * @return
     */
    @RequestMapping("startProcess")
    @ResponseBody
    public Map<String,Object> startProcess(Integer cgId){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            this.workFlowService.startProcess(cgId);
            map.put("msg","启动成功");
        }catch (Exception e){
            map.put("msg","启动失败");
            e.printStackTrace();
        }
        return map;
    }


    @RequestMapping(value = "loadCurrentUserTask",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public DataGridView loadCurrentUserTask(WorkFlowVo workFlowVo){
        return workFlowService.queryCurrentUserTask(workFlowVo);
    }

    /**
     * 跳转到办理任务的页面
     */
    @RequestMapping("toDoTask")
    public String toDoTask(WorkFlowVo workFlowVo, Model model) {
        //1,根据任务ID查询请假单的信息
        Ypcg ypcg=this.workFlowService.queryYpcgByTaskId(workFlowVo.getTaskId());
        model.addAttribute("ypcg", ypcg);
        //2,根据任务ID查询连线信息
        List<String> outcomeName=this.workFlowService.queryOutComeByTaskId(workFlowVo.getTaskId());
        model.addAttribute("outcomes", outcomeName);
        return "doTaskManager";
    }

    /**
     * 根据任务ID查询批注信息
     */
    @RequestMapping("loadAllCommentByTaskId")
    @ResponseBody
    public DataGridView loadAllCommentByTaskId(WorkFlowVo workFlowVo) {
        return this.workFlowService.queryCommentByTaskId(workFlowVo.getTaskId());
    }


    /**
     * 完成任务
     */
    @RequestMapping("doTask")
    @ResponseBody
    public Map<String,Object> doTask(WorkFlowVo workFlowVo){
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            this.workFlowService.completeTask(workFlowVo);
            map.put("msg", "任务完成成功");
        } catch (Exception e) {
            map.put("msg", "任务完成失败");
            e.printStackTrace();
        }
        return map;
    }
}
